package com.nikmesoft.android.referendumfriend.activities;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.nikmesoft.android.referendumfriend.R;
import com.nikmesoft.android.referendumfriend.service.LoginHandler;
import com.nikmesoft.android.referendumfriend.utils.CommonUtil;

public class Activity_addstt extends BaseActivity {
	EditText stt;
	private ProgressDialog dialog;
	private WSLoader loader;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addstt);
		stt = (EditText) findViewById(R.id.editStt);
		dialog = new ProgressDialog(this);
		dialog.setMessage("Loading...Please wait...");
	}

	public void callBack(View v) {
		finish();
	}

	public void callSave(View v) {
		if (stt.getText().toString().equals(""))
			return;
		Intent intent = new Intent();
		intent.putExtra("status", stt.getText().toString());
		setResult(RESULT_OK, intent);
		loader = new WSLoader();
		loader.execute(String.valueOf(LoginHandler.result.getId()), Group.t, stt
				.getText().toString());

	}

	private String callWS(String name1, String name2, String name3) {
		String body = "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soapenv:Header/><soapenv:Body>"
				+ "<addActivity soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
				+ "<AddActivityRequest xsi:type=\"rdf:AddActivityRequest\" xmlns:rdf=\"http://nikmesoft.com/apis/RDFServices/\">"
				+ "<id_user xsi:type=\"xsd:int\">"
				+ name1
				+ "</id_user>"
				+ "<id_hobby xsi:type=\"xsd:int\">"
				+ name2
				+ "</id_hobby>"
				+ "<content xsi:type=\"xsd:string\">"
				+ name3
				+ "</content>"
				+ "</AddActivityRequest>"
				+ "</addActivity>"
				+ "</soapenv:Body>" + "</soapenv:Envelope>";

		final DefaultHttpClient httpClient = new DefaultHttpClient();
		// request parameters
		HttpParams params = httpClient.getParams();
		HttpConnectionParams.setConnectionTimeout(params, 10000);
		HttpConnectionParams.setSoTimeout(params, 15000);
		// set parameter
		HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);

		// POST the envelope
		HttpPost httppost = new HttpPost(
				"http://nikmesoft.com/apis/RDFServices/index.php");
		// add headers
		httppost.setHeader("SoapAction",
				"http://nikmesoft.com/apis/RDFServices/index.php/");
		httppost.setHeader("Content-Type", "text/xml; charset=utf-8");

		String responseString = "";
		try {

			// the entity holds the request
			HttpEntity entity = new StringEntity(body);
			httppost.setEntity(entity);

			// Response handler
			ResponseHandler<String> rh = new ResponseHandler<String>() {
				// invoked when client receives response
				public String handleResponse(HttpResponse response)
						throws ClientProtocolException, IOException {

					// get response entity
					HttpEntity entity = response.getEntity();

					// read the response as byte array
					StringBuffer out = new StringBuffer();
					byte[] b = EntityUtils.toByteArray(entity);

					// write the response byte array to a string buffer
					out.append(new String(b, 0, b.length));
					return out.toString();
				}
			};

			responseString = httpClient.execute(httppost, rh);

		} catch (Exception e) {
			Log.v("Exception", e.toString());
		}

		// close the connection
		httpClient.getConnectionManager().shutdown();
		Log.i("Webservice", responseString);
		return responseString;
	}

	private class WSLoader extends AsyncTask<String, Integer, String> {

		@Override
		protected String doInBackground(String... params) {
			return callWS(params[0], params[1], params[2]);
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}

		@Override
		protected void onPostExecute(String result) {

			super.onPostExecute(result);
			try {
				dialog.dismiss();
				finish();
			} catch (Exception ex) {
				dialog.dismiss();
				toad("No Internet Connection!");
			}

		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog.show();
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
		}

	}

	public void toad(String str) {
		CommonUtil.toastNotify(this, str);
	}
}
